﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace SharedGenomics.Workbench.Annotations
{
    public class AnnotationRequestPipelineTraversalStrategy : IAnnotationRequestPipelineTraversalStrategy
    {
        private readonly IList<IAnnotationRequestStage> _stages;
        private int _currentStageIndex;

        public AnnotationRequestPipelineTraversalStrategy(IList<IAnnotationRequestStage> stages)
            : this(stages, 0)
        {
        }

        private AnnotationRequestPipelineTraversalStrategy(IList<IAnnotationRequestStage> stages, int currentIndex)
        {
            Debug.Assert(stages != null);
            this._stages = stages;
            this._currentStageIndex = currentIndex;
        }

        public void MoveNext(AnnotationRequestContext context)
        {
            if (this._currentStageIndex < this._stages.Count - 1)
            {
                this._currentStageIndex++;
                this._stages[this._currentStageIndex].Process(context, this);
            }
        }

        public IAnnotationRequestPipelineTraversalStrategy Clone()
        {
            return new AnnotationRequestPipelineTraversalStrategy(this._stages, this._currentStageIndex);
        }
    }
}
